package com.genexususa.eo.bing.data.wsSubclasses;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.AdExtensionStatus;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.BusinessGeoCodeStatus;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.LocationAdExtension;
import com.genexususa.eo.bing.data.GxItemException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class GxLocationAdExtension extends LocationAdExtension {
    private long gxCampaignId;
    private long gxId;

    public GxLocationAdExtension(Element element) throws IllegalArgumentException, GxItemException {

        // AdExtension2 parameters

        if (element.getElementsByTagName("GxCampaignId").getLength() > 0 &&
                element.getElementsByTagName("GxCampaignId").item(0).getChildNodes().getLength() > 0 &&
                Long.valueOf(((Node)element.getElementsByTagName("GxCampaignId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxCampaignId = Long.valueOf(((Node)element.getElementsByTagName("GxCampaignId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external Campaign id");
        }
        if (element.getElementsByTagName("GxLocationAdExtensionId").getLength() > 0 &&
            element.getElementsByTagName("GxLocationAdExtensionId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("GxLocationAdExtensionId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxId = Long.valueOf(((Node)element.getElementsByTagName("GxLocationAdExtensionId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external LocationAdExtension id");
        }
        if (element.getElementsByTagName("BingLocationAdExtensionId").getLength() > 0 &&
            element.getElementsByTagName("BingLocationAdExtensionId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("BingLocationAdExtensionId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setId(Long.valueOf(((Node) element.getElementsByTagName("BingLocationAdExtensionId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("Status").getLength() > 0 &&
            element.getElementsByTagName("Status").item(0).getChildNodes().getLength() > 0) {
            if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("A")) {
                this.setStatus(AdExtensionStatus.Active);
            } else if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("D")) {
                this.setStatus(AdExtensionStatus.Deleted);
            }
        }
        if (element.getElementsByTagName("Type").getLength() > 0 &&
            element.getElementsByTagName("Type").item(0).getChildNodes().getLength() > 0) {
            this.setType(((Node) element.getElementsByTagName("Type").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("Version").getLength() > 0 &&
            element.getElementsByTagName("Version").item(0).getChildNodes().getLength() > 0 &&
            Integer.valueOf(((Node)element.getElementsByTagName("Version").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setVersion(Integer.valueOf(((Node) element.getElementsByTagName("Version").item(0).getChildNodes().item(0)).getNodeValue()));
        }

        // LocationAdExtension parameters

        if (element.getElementsByTagName("Address").getLength() > 0) {
            this.setAddress(new GxAddress((Element) element.getElementsByTagName("Address").item(0)));
        }

        if (element.getElementsByTagName("CompanyName").getLength() > 0 &&
            element.getElementsByTagName("CompanyName").item(0).getChildNodes().getLength() > 0) {
            this.setCompanyName(((Node) element.getElementsByTagName("CompanyName").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("GeoCodeStatus").getLength() > 0 &&
            element.getElementsByTagName("GeoCodeStatus").item(0).getChildNodes().getLength() > 0) {
            this.setGeoCodeStatus(BusinessGeoCodeStatus.fromString(((Node) element.getElementsByTagName("GeoCodeStatus").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("GeoPoint").getLength() > 0 ) {
            this.setGeoPoint(new GxGeoPoint((Element)element.getElementsByTagName("GeoPoint").item(0)));
        }
        if (element.getElementsByTagName("IconMediaId").getLength() > 0 &&
            element.getElementsByTagName("IconMediaId").item(0).getChildNodes().getLength() > 0) {
            if (Long.valueOf(((Node) element.getElementsByTagName("IconMediaId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
                this.setIconMediaId(Long.valueOf(((Node) element.getElementsByTagName("IconMediaId").item(0).getChildNodes().item(0)).getNodeValue()));
            }
        }
        if (element.getElementsByTagName("ImageMediaId").getLength() > 0 &&
            element.getElementsByTagName("ImageMediaId").item(0).getChildNodes().getLength() > 0) {
            if (Long.valueOf(((Node) element.getElementsByTagName("ImageMediaId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
                this.setImageMediaId(Long.valueOf(((Node) element.getElementsByTagName("ImageMediaId").item(0).getChildNodes().item(0)).getNodeValue()));
            }
        }
        if (element.getElementsByTagName("PhoneNumber").getLength() > 0 &&
            element.getElementsByTagName("PhoneNumber").item(0).getChildNodes().getLength() > 0) {
            this.setPhoneNumber(((Node) element.getElementsByTagName("PhoneNumber").item(0).getChildNodes().item(0)).getNodeValue());
        }
    }

    public long getGxId() {
        return gxId;
    }

    public void setGxId(long gxId) {
        this.gxId = gxId;
    }

    public long getGxCampaignId() {
        return gxCampaignId;
    }

    public void setGxCampaignId(long gxCampaignId) {
        this.gxCampaignId = gxCampaignId;
    }
}
